//
//  CXBtPeriModel.m
//  JwPack
//
//  Created by 陈警卫 on 2025/8/12.
//

#import "CXBtPeriModel.h"

@implementation CXBtScanModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.serviceUUIDs = [NSMutableSet set];
        self.writeUUIDs = [NSMutableSet set];
        self.notifUUIDs = [NSMutableSet set];
    }
    return self;
}

@end

@implementation CXBtPeriModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.scanModel = [[CXBtScanModel alloc] init];
        self.isRetrieve = NO;
        self.isAutoReconnect = NO;
    }
    return self;
}

- (void)dealloc {
    NSLog(@"%@--dealloc", self);
}

/// 外设uuid
- (NSString *)periUuid {
    return self.peri.identifier.UUIDString;
}

/// 外设name
- (NSString *)periName {
    return self.peri.name;
}

/// 外设制造商数据
- (NSData *)periMaferData {
    NSData *maferData = [self.advDict objectForKey:CBAdvertisementDataManufacturerDataKey];
    return maferData;
}

/// 开始计时
- (void)startTimer {
    [self endTimer];
    if (self.timeout > 0) {
        __weak typeof(self) wself = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeout repeats:NO block:^(NSTimer * _Nonnull timer) {
            __strong typeof(wself) sself = wself;
            if (sself) {
                [sself timerAction];
            } else {
                [sself endTimer];
            }
            [self timerAction];
        }];
    }
}

/// 结束计时
- (void)endTimer {
    [self.timer invalidate];
    self.timer = nil;
}

/// 计时器响应
- (void)timerAction {
    if (self.didTimeoutAction) {
        NSError *error = [NSError errorWithDomain:@"连接超时" code:1 userInfo:nil];
        self.didTimeoutAction(error);
    }
    [self endTimer];
}


@end

